package cn.signalpha.modules.shop.service;

import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.shop.entity.GoodsComment;
import cn.signalpha.modules.shop.mapper.GoodsCommentMapper;
import cn.signalpha.modules.shop.vo.GoodsCommentVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 *  服务类
 *
 * @author suguangjun
 */
@Service
public class GoodsCommentService extends ServiceImpl<GoodsCommentMapper, GoodsComment> {

	/**
	 * 自定义分页
	 */
	public IPage<GoodsCommentVO> selectGoodsCommentPage(IPage<GoodsCommentVO> page, Map<String, Object> params) {
		return page.setRecords(baseMapper.selectGoodsCommentPage(page, params));
	}

	/**
	 * 自定义Wrapper
	 */
	public QueryWrapper<GoodsComment> selectGoodsCommentWrapper(Map<String, Object> params) {
		QueryWrapper<GoodsComment> wrapper = Wrappers.query();
		wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("sellerId")), GoodsComment::getSellerId, params.get("sellerId"));
		wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("goodsId")), GoodsComment::getGoodsId, params.get("goodsId"));
		wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("userId")), GoodsComment::getUserId, params.get("userId"));
		wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("isShow")), GoodsComment::getIsShow, params.get("isShow"));
		wrapper.lambda().like(ObjectUtil.isNotEmpty(params.get("userNickName")), GoodsComment::getUserNickName, params.get("userNickName"));
		wrapper.lambda().like(ObjectUtil.isNotEmpty(params.get("goodsName")), GoodsComment::getGoodsName, params.get("goodsName"));
		wrapper.lambda().ge(ObjectUtil.isNotEmpty(params.get("createTimeBegin")), GoodsComment::getCreateTime, params.get("createTimeBegin"));
		wrapper.lambda().le(ObjectUtil.isNotEmpty(params.get("createTimeEnd")), GoodsComment::getCreateTime, params.get("createTimeEnd"));
		if(ObjectUtil.isNotEmpty(params.get("isReply"))){
			if("1".equals(String.valueOf(params.get("isReply")))){
				wrapper.lambda().isNotNull(GoodsComment::getReply);
			}else if("0".equals(String.valueOf(params.get("isReply")))){
				wrapper.lambda().isNull(GoodsComment::getReply);
			}
		}
		wrapper.lambda().orderByDesc(GoodsComment::getId);
		return wrapper;
	}

	/**
	 * 数量统计
	 */
	public long countByParams(Map<String, Object> params) {
		return count(selectGoodsCommentWrapper(params));
	}

	/**
	 * 根据商品查询
	 */
	public List<GoodsComment> listByGoodsId(Long goodsId) {
		return list(Wrappers.<GoodsComment>query().lambda().eq(GoodsComment::getGoodsId, goodsId).orderByDesc(GoodsComment::getId));
	}

}
